<template>
  <div class="noto-box">
    <el-dialog width="400px" title="选择不发货地址" v-model="dialogFormVisible" :close-on-click-modal="false" :draggable="true"
      :show-close="false" @close="dialogClose">
      <div>
        <el-form ref="dialogFormRef" :model="dialogForm" :rules="dialogFormRules">
          <el-form-item prop="addressRestrict">
            <!-- <el-select style="width:100%" v-model="dialogForm.addressRestrict" multiple placeholder="请选择不发货地址">
              <el-option v-for="item in provinces" :key="item.label" :label="item.label" :value="item.label" />
            </el-select> -->
            <el-cascader style="width:100%" v-model="dialogForm.addressRestrict" collapse-tags collapse-tags-tooltip
              :options="provinces" :props="provincesProps" placeholder="请选择不发货地址" />
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="unSubmitHandle">取消</el-button>
          <el-button type="primary" @click="submitHandle">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { reactive, ref, toRefs } from 'vue'
// import provinces from '@a/js/provinces'
import api from '../api'
import { warningDeal } from '@/utils/utils'
export default {
  setup(props, context) {
    const dialogFormRef = ref(null)

    const state = reactive({
      dialogFormVisible: false,
      provinces: [],
      provincesProps: {
        value: 'systemAreaId',
        label: 'systemAreaName',
        children: 'children',
        checkStrictly: false,
        multiple: true,
      },
      dialogForm: {
        addressRestrict: [],
      },
      dialogFormRules: {
        addressRestrict: [
          { required: true, message: '请选择不发货地址', trigger: 'blur' },
        ],
      },
    })

    function dialogShow(data = []) {
      state.dialogForm.addressRestrict = [...data]
      getBelongAreaList()
      state.dialogFormVisible = true
    }

    function getBelongAreaList() {
      api.productBelongAreaList().then(res => {
        if (res.code == '200') {
          state.provinces = res.data
          setAddressRestrict(state.dialogForm?.addressRestrict || [])
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function setAddressRestrict(list) {
      list && list.map(item => {
        state.provinces && state.provinces.map(items => {
          if (item != items.systemAreaId) return
          if (!items.children || (items.children?.length == 0)) return
          items.children.map(itemss => {
            if (!state.dialogForm?.addressRestrict?.includes(itemss.systemAreaId)) {
              state.dialogForm.addressRestrict.push(itemss.systemAreaId)
            }
          })
        })
      })
    }

    function submitHandle() {
      dialogFormRef.value.validate((valid, fields) => {
        if (valid) {
          state.dialogFormVisible = false
        }
      })
    }

    function unSubmitHandle() {
      state.dialogForm.addressRestrict = false
      state.dialogFormVisible = false
    }

    function getAcode(list) {
      let myArr = []
      list && list.map(item => {
        if (typeof item == 'string') {
          myArr.push(item)
        } else {
          myArr.push(item[item.length - 1])
        }
      })
      return myArr
    }

    function dialogClose() {
      context.emit('noToDialogClose', getAcode(state.dialogForm.addressRestrict))
      dialogFormRef.value.resetFields()
    }

    return {
      ...toRefs(state),
      dialogFormRef,
      dialogClose,
      dialogShow,
      submitHandle,
      unSubmitHandle,
    }
  }
}
</script>

<style>
</style>